Skip to main content

Single Vector Summation using Einsum with Sparse Partials

This is an example of how to properly use the einsum function to compute the summation of a single vector while using sparse partial derivative.

from csdl_om import Simulatorimport numpy as npfrom csdl import Modelimport csdl

class ExampleVectorSummationSparse(Model):
    def define(self):        a = np.arange(4)        vec = self.declare_variable('a', val=a)
        self.register_output(            'einsum_summ1_sparse_derivs',            csdl.einsum_new_api(vec,                                operation=[(33, )],                                partial_format='sparse'))

sim = Simulator(ExampleVectorSummationSparse())sim.run()
print('a', sim['a'].shape)print(sim['a'])print('einsum_summ1_sparse_derivs',      sim['einsum_summ1_sparse_derivs'].shape)print(sim['einsum_summ1_sparse_derivs'])
[0. 1. 2. 3.]einsum_summ1_sparse_derivs (1,)[6.]